package com.chinaums.xm.wechat.service.wechat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.chinaums.xm.wechat.WeixinConstant;
import com.chinaums.xm.wechat.commons.MessageUtil;
import com.chinaums.xm.wechat.domain.wechat.message.Image;
import com.chinaums.xm.wechat.domain.wechat.message.ImageMessage;
import com.chinaums.xm.wechat.domain.wechat.message.Item;
import com.chinaums.xm.wechat.domain.wechat.message.NewsMessage;
import com.chinaums.xm.wechat.domain.wechat.message.TextMessage;

/**
 * 消息管理service
 * @author chenjf
 */
@Service
public class MessageService {
	
	/**
	 * 发送消息-回复文本消息
	 * @param toUserName
	 * @param fromUserName
	 * @param content
	 * @return
	 */
	public String genText(final String toUserName,final String fromUserName,final String content){
		final TextMessage textMessage = new TextMessage();
		textMessage.setFromUserName(toUserName);
		textMessage.setToUserName(fromUserName);
		textMessage.setMsgType(WeixinConstant.MESSAGE_TEXT);
		textMessage.setCreateTime(new Date().getTime());
		textMessage.setContent(content);
		return MessageUtil.textMessageToXml(textMessage);
	}
	
	/**
	 * 发送消息-回复图片消息
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 */
	public String genImage(String toUserName, String fromUserName) {
		final ImageMessage imageMessage = new ImageMessage();
		imageMessage.setFromUserName(toUserName);
		imageMessage.setToUserName(fromUserName);
		imageMessage.setCreateTime(new Date().getTime());
		imageMessage.setMsgType(WeixinConstant.MESSAGE_IMAGE);
		Image image = new Image();
		image.setMediaId("SuAeTgYSammpELcnSClb8r1bVWYVeucVS8MKs4byV7IHYCd6JOuO-Vxty3Z9ejYN");
		imageMessage.setImage(image);
		return MessageUtil.imageMessageToXml(imageMessage);
	}
	
	/**
	 * 回复图文消息
	 * @param toUserName
	 * @param fromUserName
	 * @param content
	 * @return
	 */
	public String genNews(final String toUserName,final String fromUserName){
		final NewsMessage newsMessage = new NewsMessage();
		newsMessage.setFromUserName(toUserName);
		newsMessage.setToUserName(fromUserName);
		newsMessage.setMsgType(WeixinConstant.MESSAGE_NEWS);
		newsMessage.setCreateTime(new Date().getTime());
		final List<Item> articles = new ArrayList<>();
		final Item item = new Item();
		item.setTitle("图文消息回复测试");
		item.setDescription("图文消息回复测试");
		item.setPicUrl("http://localhost:8089/image/baidu.png");
		item.setUrl("https://www.baidu.com");
		articles.add(item);
		newsMessage.setArticleCount(articles.size());
		newsMessage.setArticles(articles);
		return MessageUtil.newsMessageToXml(newsMessage);
	}
}
